package  
{
	import org.flixel.FlxEmitter;
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.FlxSprite;
	
	/**
	 * ...
	 * @author maliu
	 */
	public class Enemy extends FlxSprite 
	{
		[Embed(source = "../res/enemy.png")]
		protected var EnemyImgRef:Class;
		[Embed(source = "../res/enemygibs.png")]
		protected var GibsImgRef:Class;
		
		protected static const ENEMY_SPEED:Number = 20;
		protected static const ENEMY_HEALTH:int = 5;
		
		protected var p_startX:Number;
		protected var p_maxHorizontalMovement:Number; //
		protected var p_gibs:FlxEmitter; //
		
		public function Enemy(_startX:Number, _startY:Number, _maxHorizontalMovement:Number) 
		{
			super(_startX, _startY);
			this.loadGraphic(EnemyImgRef, true, false, 13, 13);
			
			this.y = _startY - this.height;
			this.p_startX = _startX;
			
			this.p_maxHorizontalMovement = _maxHorizontalMovement - this.width;
			this.velocity.x = ENEMY_SPEED;
			this.health = ENEMY_HEALTH;
			//
			this.p_gibs = new FlxEmitter(0, 0);
			this.p_gibs.createSprites(GibsImgRef, 5, 16, false, 0, 0);
			this.p_gibs.setXSpeed( -100, -100);
			this.p_gibs.setYSpeed( -100, -150);
			this.p_gibs.gravity = 300;
			FlxG.state.add(p_gibs);
			
			this.addAnimation("move", [0, 1], 12);
			this.play("move");
		}
		// override FlxObject.hitSide()
		override public function hitSide(_contact:FlxObject, _velocit:Number):void
		{
			this.velocity.x = -this.velocity.x;
		}
		// override FlxObject.kill()
		override public function kill():void
		{
			super.kill();
			
			p_gibs.x = this.x;
			p_gibs.y = this.y;
			
			p_gibs.start(true, 5);
		}
		// override FlxSprite.update()
		override public function update():void
		{
			if ((this.x - this.p_startX) >= p_maxHorizontalMovement)
			{
				this.x = p_startX + p_maxHorizontalMovement;
				this.velocity.x = -ENEMY_SPEED;
			}
			else if ((this.x - p_startX) <= 0)
			{
				this.x = p_startX;
				this.velocity.x = -ENEMY_SPEED;
			}
			
			super.update();
		}
	}

}